<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <!-- get 请求 -->
    <button onclick="getAxios()">getAxios</button>
    <br>
    <br>
    <!-- post 请求 -->
    <button onclick="postAxios()">postAxios</button>
    <br>
    <br>
    <!-- com 请求 -->
    <button onclick="comAxios()">comAxios</button>
    <br>
    <br>
    <!-- all 请求 -->
    <button onclick="allAxios()">allAxios</button>
    <br>
    <br>
    <!-- 自定义请求 请求 -->
    <button onclick="cusAxios()">cusAxios</button>
    <br>
    <br>
    <!-- 拦截 请求 -->
    <button onclick="interceptorAxios()">interceptorAxios</button>
    <br>
    <br>




    <!-- 引入 axios -->
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script>
        // 拦截 请求
        function interceptorAxios(){
            var instance = axios.create({
                url: 'http://vue.studyit.io/api/postcomment/43',
                timeout: 3000,
                method: 'post',
                params: {
                    content: '自定义请求'
                },
                data: {
                    content: '自定义'
                }
            });
            instance.interceptors.request.use(config=>{
                console.log( '1111' );
                return config;
            });
            instance.request().then(data=>{
                console.log(data);
            }).catch(err=>{

            });
        }
        // 自定义请求
        function cusAxios() {
            var instance = axios.create({
                url: 'http://vue.studyit.io/api/postcomment/43',
                timeout: 3000,
                method: 'post',
                params: {
                    content: '自定义请求'
                }
            });
            instance.get();
            instance.request();
            
        }
        // axios多个并发请求
        function allAxios() {
            function getaxios1() {
                return axios.get('http://vue.studyit.io/api/getcomments/43?pageindex=1');
            }

            function getaxios2() {
                return axios.get('http://vue.studyit.io/api/getcomments/44?pageindex=1');
            }
            axios.all([getaxios1(), getaxios2()])
                .then(axios.spread(function (data1, data2) {
                    console.log(data1);
                    console.log(data2);
                }))
        }
        // comAxios 请求
        function comAxios() {
            // 通过 config 对象来配置axios请求
            axios({
                    method: 'post',
                    url: 'http://vue.studyit.io/api/postcomment/43',
                    data: {
                        content: 'top'
                    }
                })
                .then(result => {
                    console.log(result);
                })
                .catch(err => {
                    console.log(err);
                })
        }
        // post 请求 
        // post请求 设置请求头
        axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';

        function postAxios() {
            axios.post('http://vue.studyit.io/api/postcomment/' + 43, {
                content: '评论本文'
            }).then(response => {
                console.log(response);
            }).catch(error => {
                console.log(error);
            })
        }
        // get 请求
        function getAxios() {
            axios.get('http://vue.studyit.io/api/getcomments/' + 43, {
                    params: {
                        pageindex: 1
                    }
                })
                .then(result => {
                    console.log(result);
                })
                .catch(err => {
                    console.log(err);

                })
        }
    </script>
</body>

</html>